package cn.chinaunicom.sdsi.frm.workflow.task.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.chinaunicom.sdsi.frm.base.BaseController;
import cn.chinaunicom.sdsi.frm.sys.entity.SysStaffOrg;
import cn.chinaunicom.sdsi.frm.sys.service.MainService;
import cn.chinaunicom.sdsi.frm.workflow.bean.TaskVO;
import cn.chinaunicom.sdsi.frm.workflow.task.service.ActTaskService;
import net.sf.json.JSONObject;

@Controller("workflowController")
@RequestMapping(value = "/workflow")
public class WorkflowController extends BaseController {

	private final Log log = LogFactory.getLog(WorkflowController.class);

	/**
	 * 工作流任务相关的业务逻辑组件
	 */
	private ActTaskService actTaskService;
	/**
	 * 此接口用于获取全部岗位
	 */
	private MainService mainService;

	/**
	 * 待办任务页面跳转
	 * 
	 * @return 视图解析路径
	 */
	@RequestMapping("/taskToDoList")
	public ModelAndView taskToDoList() {

		ModelAndView mv = new ModelAndView();
		mv.setViewName("frm/workflow/tasklist/task-todo");
		mv.addObject("currentId", this.getCurrentSysStaffOrg().getStaffOrgId().toString());
		log.info("跳转到工作流待办基础页面！");

		return mv;
	}

	/**
	 * 待办任务详细页面跳转
	 * 
	 * @param vo
	 *            任务实体
	 * @return 带数据视图
	 */
	@RequestMapping("/taskToDoDetail")
	public ModelAndView taskToDoDetail(TaskVO vo) {

		ModelAndView mv = new ModelAndView();
		// 判断如果当前任务已经完成，直接返回待办任务列表
		if (actTaskService.isFinishedTask(vo.getTaskId())) {
			return taskToDoList();
		}

		mv.addObject("taskDetail", vo);
		mv.setViewName("frm/workflow/taskdetail/task-todo");

		Map<String, Boolean> map = actTaskService.getLinkCfg(vo.getProcessDefinitionKey(), vo.getTaskDefinitionKey());
		mv.addObject("buttonBack", map.get("buttonBack"));
		mv.addObject("buttonTurn", map.get("buttonTurn"));
		mv.addObject("buttonBreak", map.get("buttonBreak"));
		mv.addObject("buttonQuick", map.get("buttonQuick"));
		mv.addObject("isCounterSign", map.get("isCounterSign"));
		boolean canQuickPush = actTaskService.isBackForQuick(vo.getProcessInstanceId(), vo.getTaskId());
		mv.addObject("canQuickPush", canQuickPush);

		// 标记已被受理人预览无法再撤回
		actTaskService.viewTask(vo.getProcessInstanceId(), vo.getTaskId());
		log.info("标记已经浏览不可撤回并跳转到工作流待办详细页面！");

		return mv;
	}

	/**
	 * 已办任务页面跳转
	 * 
	 * @return 视图解析路径
	 */
	@RequestMapping("/taskHasDoneList")
	public ModelAndView taskHasDoneList() {

		ModelAndView mv = new ModelAndView();
		mv.setViewName("frm/workflow/tasklist/task-hasdone");
		mv.addObject("currentId", this.getCurrentSysStaffOrg().getStaffOrgId().toString());
		log.info("跳转到工作流已办基础页面！");

		return mv;
	}

	/**
	 * 已办任务详细页面跳转
	 * 
	 * @param vo
	 *            任务实体
	 * @return 带数据视图
	 */
	@RequestMapping("/taskHasDoneDetail")
	public ModelAndView taskHasDoneDetail(TaskVO vo) {

		ModelAndView mv = new ModelAndView();
		mv.addObject("taskDetail", vo);
		mv.setViewName("frm/workflow/taskdetail/task-hasdone");

		boolean canWithDraw = actTaskService.isCanWithDraw(vo.getProcessInstanceId(), vo.getTaskId());
		mv.addObject("canWithDraw", canWithDraw);
		log.info("判断是否可以撤回打标，并跳转到工作流已办详细页面！");

		return mv;
	}

	/**
	 * 翻页查找待办任务
	 * 
	 * @param vo
	 *            查询条件封装
	 * @param start
	 *            开始条数
	 * @param length
	 *            每页长度
	 * @return 分页查询结果
	 */
	@RequestMapping("/taskToDo")
	@ResponseBody
	public JSONObject taskToDo(TaskVO vo, int start, int length) {

		// 查询当前登录人全部待办任务
		vo.setAssigneeIds(this.getSysOrgIds());
		log.info("筛选当前登录用户当前岗位下的全部待办任务。");

		return actTaskService.listToDo4Page(vo, start, length);
	}

	/**
	 * 翻页查找已办任务 前台使用jquery-datatable
	 * 
	 * @param vo
	 *            查询条件封装
	 * @param start
	 *            开始条数
	 * @param length
	 *            每页长度
	 * @return 分页查询结果
	 */
	@RequestMapping("/taskHasDone")
	@ResponseBody
	public JSONObject taskHasDone(TaskVO vo, int start, int length) {

		// 查询当前登录人全部已办任务
		vo.setAssigneeIds(this.getSysOrgIds());
		log.info("筛选当前登录用户当前岗位下的全部已办任务。");

		return actTaskService.listHasDone4Page(vo, start, length);
	}

	/**
	 * 获取当前登录人全部岗位ID数组
	 * 
	 * @return 全部岗位ID数组
	 */
	private String[] getSysOrgIds() {
		// 取当前登录人全部岗位
		List<SysStaffOrg> list = mainService.findSysStaffOrg();
		String[] result = new String[list.size()];

		for (int i = 0; i < list.size(); i++) {
			result[i] = String.valueOf(list.get(i).getStaffOrgId());
		}

		return result;
	}

	@Autowired
	public void setActTaskService(ActTaskService actTaskService) {
		this.actTaskService = actTaskService;
	}

	@Autowired
	public void setMainService(MainService mainService) {
		this.mainService = mainService;
	}

}
